package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.SaleCTR;
import ModelLayer.*;


public class SaleUI
{
    private SaleCTR saleCTR;
    public SaleUI()
    {
        saleCTR = new SaleCTR();
    }
    public void start()
    {
        saleMenu();
    }
    private void saleMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeSaleMenu();
            if(choise == 1)
            {
                startCreateSale();
            }
            else
            {
                if(choise == 2)
                {
                    startFindsSale();
                }
                exit = true;
            }
        }
    }
    private int writeSaleMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Sale Menu ***");
        System.out.println(" (1) Create Sale");
        System.out.println(" (2) Find Sale");
        System.out.println(" (3) Return");
        System.out.print("\n Make your choise: ");
            
        int choise = keyboard.nextInt();
        return choise;
    }
        /**
     * User input section
     * 
     * inputBuyerPhone(), inputs the BuyerPhone via user interaction.
     * inputPostalCode(), inputs the persons postal code via user interaction.
     * inputCity(), inputs the persons city via user interaction.
     * inputPhone(), inputs the persons phone via user interaction.
     * inputAddress(), inputs the persons address via user interaction.
     */
    public String inputBuyerPhone()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write phone number of customer: ");
        String phone = keyboard.nextLine();
        return phone;
    }
    public String inputStaffPhone()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write phone number of staff: ");
        String phonestaff = keyboard.nextLine();
        return phonestaff;
    }
    public String inputSaleDate()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write phone number of staff: ");
        String saleDate = keyboard.nextLine();
        return saleDate;
    }
    public int inputSpecimenID()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Serial Number of the wanted Product specimen: ");
        int serial = keyboard.nextInt();
        return serial;
    }
    public int inputProductID()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write ID of the wanted Product: ");
        int id = keyboard.nextInt();
        return id;
    }
    public int inputSaleID()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write ID of the wanted sale: ");
        int saleID = keyboard.nextInt();
        return saleID;
    }
    public void startCreateSale()
    {
        int id = inputProductID();
        int serial = inputSpecimenID();
        String phone = inputBuyerPhone();
        String phonestaff = inputStaffPhone();
        int saleID = inputSaleID();
        saleCTR.createSale(id, serial, saleID, phone, phonestaff);
     
    }
    private void startFindsSale()
    {
        String personPhone = inputBuyerPhone();
        
        ArrayList<Sale> foundSales = saleCTR.findAllSales(personPhone);
        
        try
        {
            if (foundSales != null)
            {
                
                int index = 0;
                for(Sale sale : foundSales)
                {
                    if (index == 0)
                    {
                        System.out.println(" -- Personal Information --");
                        System.out.println(" " + sale.getCustomer().getPhoneNo());
                        System.out.println(" " + sale.getCustomer().getAddress());
                        System.out.println(" " + sale.getCustomer().getLastName() + ", " + sale.getCustomer().getFirstName());
                        System.out.println("\n -- Sale Conducted --");
                    }
                    System.out.println(" Medarbejder: " + sale.getStaff().getPhoneNo());
                    System.out.println(" Date: " + sale.getSpecimen().getSerialNr());
                    
                    index++;
                }
            
                System.out.println("\n Press the enter key to return to menu");
                Scanner sc = new Scanner(System.in);
                while(!sc.nextLine().equals(""));
            }
            else
            {
                System.out.println("\n ** No lend information was found on that phonenumber! **");
                Thread.sleep(1500);
            }
        }
        catch (Exception e)
        {
            System.out.println("\n ** An error has occurred! **");
        }
    }
   
}
